package se.webbzon.boltzmann.game.object.platform;

import java.nio.ByteBuffer;

import se.webbzon.boltzmann.game.object.waypoint.PathProgramEnum;
import se.webbzon.boltzmann.game.object.waypoint.Waypoint;
import se.webbzon.boltzmann.object.geometry.MaskedGeometryDeflation;
import se.webbzon.boltzmann.object.unknown.Unknown;
import se.webbzon.oschi01.friendly.Friendly;
import se.webbzon.oschi01.worldobject.WorldObject;
import se.webbzon.oschi01.worldobject.WorldStaticObject;
import se.webbzon.oschi01.worldobject.appearance.Appearance3DException;

public class PlatformDeflation extends MaskedGeometryDeflation {
	
	private PathProgramEnum program;
	
	private int time, duration;

	/*============================================================
	Constructors
	============================================================*/
	
	public PlatformDeflation(int deflationProperties, WorldObject[] dependencies) {
		super(deflationProperties,dependencies);
	}
	
	public PlatformDeflation(int deflationProperties, Platform platform, Waypoint waypoint) {
		super(	deflationProperties,
				(waypoint == null) ? null : new Waypoint[] {waypoint},
				platform);
		program = platform.getProgram();
		time = platform.pos();
		duration = platform.getCycleDuration();
	}
	
	/*============================================================
	Public Methods
	============================================================*/
	
	@Override public void inflate(ByteBuffer buff) {
		super.inflate(buff);
		program = PathProgramEnum.values()[buff.getInt()];
		time = buff.getInt();
		duration = buff.getInt();
	}

	@Override public ByteBuffer deflate(int allocate) {
		ByteBuffer buff = super.deflate(allocate + 12);
		buff.putInt(program.ordinal());
		buff.putInt(time);
		buff.putInt(duration);
		return buff;
	}
	
	/*============================================================
	Friendly Methods
	============================================================*/
	
	@Friendly WorldStaticObject makePlatform() {
		WorldStaticObject instance;
		try {
			Waypoint waypoint = dependencies() > 0 ? 
					(Waypoint) getDependency(0) : null;
			
			Platform platform = 
					new Platform(	makeGeometryAppearance(),isVisible(),
									makeGeometryMask(),isSolid(),false,
									program,waypoint);
			platform.setCycleDuration(duration);
			platform.seek(time);
			instance = platform;
		} catch (Appearance3DException e) {
			instance = new Unknown();
		}
		
		instance.setPosition(getX(),getY(),getZ());
		instance.setSize(getWidth(), getHeight(), getDepth());
		instance.setRotation(getEX(), getEY(), getEZ(), getAngle());
		
		return instance;
	}

}
